﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="sys_menu_tree.aspx.cs" Inherits="Ccit.SysPlat.Purview.sys_menu_tree" %>
<%@ Register assembly="Microsoft.Web.UI.WebControls, Version=1.0.2.226, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="Microsoft.Web.UI.WebControls" tagprefix="iewc" %>

<html>
<head runat="server">
    <title></title>
    <script language="javascript" src="../script/jquery.js" type="text/javascript"></script>
    <script language="javascript" src="../script/StringClass_Ext.js" type="text/javascript"></script>
    <style>
		.skin { BORDER-RIGHT: buttonhighlight 2px outset; BORDER-TOP: buttonhighlight 2px outset; FONT-SIZE: 9pt; VISIBILITY: hidden; BORDER-LEFT: buttonhighlight 2px outset; WIDTH: 100px; CURSOR: default; BORDER-BOTTOM: buttonhighlight 2px outset; FONT-FAMILY: Arial, Helvetica, sans-serif; POSITION: absolute; BACKGROUND-COLOR: menu; TEXT-ALIGN: left }
		.menuitems { PADDING-RIGHT: 5px; PADDING-LEFT: 5px; PADDING-TOP: 5px; PADDING-BOTTOM: 5px;}
	</style>
</head>
<body onclick="hideMenu()">
    <form id="form1" runat="server"> 
    <input type="hidden" id="_hidOgUpMenuId" runat="server" />
    <input type="hidden" id="_hidAtUpMenuId" runat="server" />
    <input type="hidden" id="_hidAddNewSign" runat="server" /> <!-- 新增记录标志 -->
    <input type="hidden" id="_hidDelMenuId" runat="server" />  
    <input type="hidden" id="_hidCurMenuExpr" runat="server" />
    <input type="hidden" id="_hidExpandSign" runat="server" />
    <input type="hidden" id="_hidLevelStruct" runat="server" />
    <input type="hidden" id="_hidSourceType" runat="server" />    
    <asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server">
    </asp:ScriptManager>    
    <iewc:TreeView ID="trvMenu" runat="server">
    </iewc:TreeView>
    <div id="root" class="skin" style="LEFT:0px; VISIBILITY:hidden; WIDTH:110px; TOP:0px; HEIGHT:35px">
        <div  onclick="clickItem()" onMouseover="highlighItem()" onMouseout="lowlightItem()" style="BORDER-BOTTOM: window 2px inset;" class="menuitems" func="AddSubMenu">            
                <img src="../images/toolbar/addnew_bq.gif" border="0" />&nbsp; 新增子菜单            
        </div>
        <div onclick="clickItem()" onMouseover="highlighItem()" onMouseout="lowlightItem()" class="menuitems" func="DeleteMenu">
            <img src="../images/toolbar/del_bq.gif" border="0" />&nbsp; 删除菜单
        </div>
    </div>
    <input type="button" id="btnTrigger" onclick="doTrigger()" style="display:none" value="触发事件" />
    <input type="button" id="btnDelTrigger" onclick="doDelTrigger()" style="display:none" value="删除触发事件" />
    </form>
    <script type="text/javascript">
    <!--
        var menuskin = "skin";
		var node = null;
		var menuname = "root";		
		
		/* 用于更新变更前后节点,定义的全局变量 */
		var ctrlOgNode = null;
		var ctrlDtNode = null;
		var vOgNodeIndex;
		var vDtNodeIndex;
		
		function hideMenu() 
		{		    
			var obj = document.getElementById(menuname);
			if (null != obj && typeof(obj) != "undefined")
				document.getElementById(menuname).style.visibility = "hidden";
		}
		
		function highlighItem() 
		{
			if (event.srcElement.className == "menuitems") 
			{
				event.srcElement.style.backgroundColor = "highlight";
				event.srcElement.style.color = "white";
			}
		}
		function lowlightItem() 
		{
			if (event.srcElement.className == "menuitems") 
			{
				event.srcElement.style.backgroundColor = "";
				event.srcElement.style.color = "black";
				window.status = "";
			}
		}
		function clickItem() 
		{								
			if(node != null)
			{
				var vNodeId = node.getAttribute("ID");
				//alert(vNodeId);				
				var nodeType = node.getAttribute("type").toUpperCase();
				var vNodeIdArray = vNodeId.split("_");
				var vMenuId = vNodeIdArray[0];
												
				if (event.srcElement.className == "menuitems" && event.srcElement.disabled != true) 
				{
					var sFuncName = event.srcElement.getAttribute("func");					
					switch (sFuncName)
					{
						case "AddSubMenu":	//新增子菜单
						    var vPageParam = "upmenuid=" + vMenuId;
						    var encodeParam = base64encode(vPageParam);
							parent.frames[1].location.href = "./sys_menu_edit.aspx?" + encodeParam;								
							break;
						case "DeleteMenu": //删除菜单						    
						    if(!confirm("删除当前菜单,是否继续?"))
                                return;
                            
                            var nodeIndex = node.getNodeIndex();                            
                            PageMethods.DeleteMenu(vMenuId,nodeIndex,ClientCallBack);						    
							break;
						default:
							break;
					}						
				}
			}
		}
		
		function trvMenu.onexpand()
		{	
		    /* Step 1：找到当前点击的TreeNode */	    
		    var nodeIndex = event.treeNodeIndex;			    	
			if (typeof(nodeIndex) == "undefined"){			
				node = null;
				return false;
			}
			
			trvMenu.selectedNodeIndex = nodeIndex;
			node = trvMenu.getTreeNode(nodeIndex);
						
			/* Step 2: 获取菜单ID，获取菜单下的子菜单 */			
			var vNodeId = node.getAttribute("ID");
			var vNodeIdArray = vNodeId.split("_")
			var vMenuId = vNodeIdArray[0];
			var vLoadSign = vNodeIdArray[1];
						
			if(vMenuId != "0" && vLoadSign == "0") {
			    var oSubNodes = node.getChildren();
			    var oSubNode;
			    for(var i = oSubNodes.length - 1; i >= 0; i --) {
			        oSubNode = oSubNodes[i];
			        oSubNode.remove();
			    }
			    
			    var expandSign = $get("_hidExpandSign").value;//是否逐级展开 1：是
			    var levelStruct = $get("_hidLevelStruct").value;
			    var sourceType = $get("_hidSourceType").value;
			    PageMethods.GetSubMenus(vMenuId, nodeIndex, expandSign, levelStruct, sourceType, ClientCallBack);
			    
			}
			hideMenu();
		}
		
		function trvMenu.oncollapse()
		{
			hideMenu();
		}
		
		function trvMenu.oncontextmenu()
		{				
			hideMenu();
			var nodeIndex = event.treeNodeIndex;							
			if (typeof(nodeIndex) == "undefined")
			{
				node = null;
				return false;
			}
			trvMenu.selectedNodeIndex = nodeIndex;
			node = trvMenu.getTreeNode(nodeIndex);			
			var vNodeId = node.getAttribute("ID");			
			var vNodeIdArray = vNodeId.split("_");
			var vMenuId = vNodeIdArray[0];
			var vLay = vNodeIdArray[2];
			
			if(vMenuId == "0") return;  //根节点不显示右键菜单
			
			var shortCutMenu = $get("root");
			if(vLay == "3") {			    
			    shortCutMenu.children(0).disabled = "disabled";
			}
			else {
			    shortCutMenu.children(0).disabled = "";
			}
			
			
			var obj = document.getElementById("root");				
			if (null == obj || typeof(obj) == "undefined")
			{
				node = null;
				return false;
			}
			
			var rightedge = document.body.clientWidth - event.clientX;		//右边距
			var bottomedge = document.body.clientHeight - event.clientY;	//底边距
			
			if (rightedge < obj.offsetWidth)
				obj.style.left = document.body.scrollLeft + 5 + event.clientX - obj.offsetWidth;
			else
				obj.style.left = document.body.scrollLeft + 5 + event.clientX ;
			
			if (bottomedge < obj.offsetHeight)
				obj.style.top = document.body.scrollTop + 5 + event.clientY - obj.offsetHeight ;
			else
				obj.style.top = document.body.scrollTop + 5 + event.clientY ;
											
			obj.style.visibility = "visible";
			return false;
		}	
		
		function ClientCallBack(vResult) {		 
            eval("var resultObj=" + vResult);
            var operType = resultObj.OPERTYPE;       
            var nodeIndex;
            switch (operType) {
                case "DYNLOADNODES":  //动态加载树的子节点
                    nodeIndex = resultObj.NODEINDEX;
                    var subMenuArray = resultObj.MENUS;
                    var expandSign = resultObj.EXPSIGN;
                    var levelStruct = resultObj.LVSTRUCT;
                    var parentNodeId = resultObj.CURNODEID;
                    
                    createSubTreeNodes(nodeIndex, subMenuArray);

                    if (expandSign == "1") {//是逐级展开
                        var parentIdArr = levelStruct.split(",");
                        var iLen = parentIdArr.length;
                        var bGet = false;
                        for (var i = 0; i < iLen; i++) {
                            var colId = parentIdArr[i];
                            if (!bGet) {
                                if (colId == parentNodeId) {
                                    if (i == (iLen - 1)) {//如果到最底层，则逐级展开标志0
                                        $get("_hidExpandSign").value = "0";
                                    }
                                    bGet = true;
                                    continue;
                                }
                            }
                            else {//true，展开下级
                                var ctrlNode = findTreeNode(colId);
                                var nodeAttrId = ctrlNode.getAttribute("ID");
                                var vNodeIdArray = nodeAttrId.split("_")
                                if (vNodeIdArray[1] == "1") {
                                    if (i == (iLen - 1)) {
                                        ctrlNode.setAttribute("EXPANDED", true);
                                    }
                                    else
                                        continue;
                                }
                                else {
                                    ctrlNode.setAttribute("EXPANDED", true);
                                    break;
                                }
                            }
                        }
                    }
                    break;
                case "MENUDELETE":  //删除菜单
                    if (parseInt(resultObj.RSTSIGN) > 0) {
                        alert(resultObj.RSTMSG);
                        nodeIndex = resultObj.NODEINDEX;
                        var node = $get("trvMenu").getTreeNode(nodeIndex);
                        node.remove();
                        parent.frames[1].initAddNew();
                    }
                    else {
                        var msg = resultObj.RSTMSG.replace(/\\r\\n/g, "\r\n");
                        alert(msg);
                    }

                    break;
                case "GETSTRUCT":  // get node struct tree ids
                    var levelStruct = resultObj.RSTIDS;
                    if (levelStruct == "") {
                        alert("操作失败:请稍后重新尝试操作!");
                        return;
                    }
                    
                    $get("_hidLevelStruct").value = levelStruct;
                    var parentIdArr = levelStruct.split(",");
                    var iLen = parentIdArr.length;
                    var menuId = "";
                    var ctrlNode;
                    var nodeAttrId = "";
                    for (var i = 0; i < iLen; i++) {
                        menuId = parentIdArr[i];
                        ctrlNode = findTreeNode(menuId);
                        nodeAttrId = ctrlNode.getAttribute("ID");

                        var vNodeIdArray = nodeAttrId.split("_")
                        if (vNodeIdArray[1] == "1") { //判断最老祖宗是否展开过 1--：展开过
                            continue;
                        }
                        else {
                            ctrlNode.setAttribute("EXPANDED", true); //没展开连续展开调tvColumn.onexpand()方法展开最老祖宗
                            break;
                        }
                    }
                    break;                        
            }
            
            resultObj = null;
        }
        
        function createSubTreeNodes(nodeIndex,subMenuArray) {                     
			var node;
			if(nodeIndex == vOgNodeIndex) {
			    node = ctrlOgNode;
			}
			else if(nodeIndex == vDtNodeIndex) {
			    node = ctrlDtNode;
			}
			else {
			    node = $get("trvMenu").getTreeNode(nodeIndex);
			}    
			
            var vMenuId;
            var vMenuName;
            var vHasChild;
            var vSysSign;
            var vLaySign;
            var vRecOriType;
            var vShowText;
            var vNavigateUrl;
            var oSubNode;
            var oEmptyNode;
            
            var itemCount = subMenuArray.length;
            var subMenu;            
            var vPageParam;
            var encodeParam;
            for(var i = 0; i < itemCount; i ++) {
                subMenu = subMenuArray[i];
                vMenuId = subMenu.menuId;
                vMenuName = subMenu.menuName;
                vHasChild = subMenu.isHasChild;
                vSysSign = subMenu.sysSign;
                vLaySign = subMenu.lay;
                vRecOriType = subMenu.oriType;
                vSourceType = subMenu.sourceType;
                
                vPageParam = "menuId=" + vMenuId + "&sys=" + vSysSign + "&recOri=" + vRecOriType + "&sourceType=" + vSourceType;
                encodeParam = base64encode(vPageParam);
                vNavigateUrl = "./sys_menu_edit.aspx?" + encodeParam;
                oSubNode = $get("trvMenu").createTreeNode();
		        oSubNode.setAttribute("ID",vMenuId + "_" + "0" + "_" + vLaySign);
		        if(vSysSign == "0") {
		           vShowText = vMenuName;
		        }
		        else {
		           vShowText = vMenuName + " [<font color='red'>系统</font>]";
		        }
		        oSubNode.setAttribute("Text",vShowText);
		        oSubNode.setAttribute("Type","root");
		        oSubNode.setAttribute("Target","ifrmEdit");
		        oSubNode.setAttribute("ImageUrl","../images/toolbar/menu.gif");
		        oSubNode.setAttribute("NavigateUrl",vNavigateUrl);
		         
		        if(vHasChild == "1") {  //如果该节点下有子节点，则在节点下增加一个空节点
		          oEmptyNode = $get("trvMenu").createTreeNode();  
		          oSubNode.add(oEmptyNode); 
		        }
		        node.add(oSubNode);
            }
            
            /* 将节点的加载标志置为1 */
		    var vNodeId = node.getAttribute("ID");
		    var vNodeIdArray = vNodeId.split("_")
			vMenuId = vNodeIdArray[0];
			vLaySign = vNodeIdArray[2];
			node.setAttribute("ID",vMenuId + "_" + "1" + "_" + vLaySign);	
        }
        
        function findTreeNode(pMenuId) {
            var ctrlTree = document.getElementById("trvMenu");		    
		    
		    var ctrlRoot = ctrlTree.getTreeNode("0"); 
		    var ctrlChildNodes = ctrlRoot.getChildren();
		 
		    var ctrlFindNode = recursionFind(ctrlChildNodes,pMenuId);
		    return ctrlFindNode;
        }
        
        function recursionFind(ctrlChildNodes,pMenuId) {
            var iLen = ctrlChildNodes.length;
            var ctrlTreeNode;
		    var vNodeId;
		    var vNodeIdArray;
		    var vDeptId;
		    var ctrlSubChildNodes;
		    var bFind = false;
		    
            for(var i = 0; i < iLen; i ++) {
		        ctrlTreeNode = ctrlChildNodes[i];
		        vNodeId = ctrlTreeNode.getAttribute("ID");    
		        vNodeIdArray = vNodeId.split("_")
		        vMenuId = vNodeIdArray[0];
		        if(vMenuId == pMenuId) {		           
		            bFind = true;
		            break;       
		        }
		        else {
		            ctrlSubChildNodes = ctrlTreeNode.getChildren();
		            ctrlTreeNode = recursionFind(ctrlSubChildNodes,pMenuId);
		            if(null != ctrlTreeNode) {
		                bFind = true;
		                break;
		            }   
		        }
		    }
            
            if(bFind)
                return ctrlTreeNode;
            else
                return null;
        }
        
        function doTrigger() {
            doOperOriParentNode();  // update origin parent node
            doOperDtParentNode();   // update destination parent node
		}
		
		function doOperOriParentNode() {
		    var vOriUpMenuId = $get("_hidOgUpMenuId").value; 
		    var vUpMenuId = $get("_hidAtUpMenuId").value;		
		    var isAddNew = $get("_hidAddNewSign").value;
		    
		    if(isAddNew=="1"){//编辑
		        // find current node position index
		        eval("var curNodeObj=" + $get("_hidCurMenuExpr").value);
		        var curNodeId = curNodeObj.menuId;
                var curNode = findTreeNode(curNodeId);
                
		        if(vUpMenuId == vOriUpMenuId) {  //parent node no change
		            curNode.setAttribute("Text",curNodeObj.menuName);
		            return;
		        }
		        
		        ctrlOgNode = null;		    
		        vOgNodeIndex = "";
		        // get root node object
		        var ctrlTree = document.getElementById("trvMenu");	
		        var ctrlRoot = ctrlTree.getTreeNode("0");
    		    
		        /* 获取原始上级节点对象 */
		        if(vOriUpMenuId != "" && vOriUpMenuId!="0") {
		            ctrlOgNode = findTreeNode(vOriUpMenuId);
		            vOgNodeIndex = ctrlOgNode.getNodeIndex();
		        }
		        else {
		            ctrlOgNode=ctrlRoot;
		            vOgNodeIndex= "0";
		        }
                
                // delete origin node
                curNode.remove();               
                curNodeObj = null;
		    }
		     
		}
		function doOperDtParentNode() {
		    var vOriUpMenuId = $get("_hidOgUpMenuId").value; 
		    var vUpMenuId = $get("_hidAtUpMenuId").value;    
		    var isAddNew = $get("_hidAddNewSign").value;
		    
		    /* 初始化全局变量 */
		    
		    ctrlDtNode = null;		    
		    vDtNodeIndex = "";
		    
		    // get root node object
            var ctrlTree = document.getElementById("trvMenu");	
            var ctrlRoot = ctrlTree.getTreeNode("0");
		    
		    
            /* 获取新上级节点对象 */
            if(vUpMenuId != "" && vUpMenuId!="0") {		        
                ctrlDtNode = findTreeNode(vUpMenuId);		
                if(null == ctrlDtNode) {
                    expandDtNode(vUpMenuId);
                    return;
                }        
                vDtNodeIndex = ctrlDtNode.getNodeIndex();
            }
            else {	
                ctrlDtNode=ctrlRoot;	
                vDtNodeIndex= "0";  
            }
                        
            var dtNodeId = ctrlDtNode.getAttribute("ID");
			var vNodeIdArray = dtNodeId.split("_")
			var vMenuId = vNodeIdArray[0];
			var vLoadSign = vNodeIdArray[1];
			
			var curNodeExpr = $get("_hidCurMenuExpr").value;
            eval("var curNodeObj=" + curNodeExpr);    
            var subMenuArray = [];
            subMenuArray.push(curNodeObj);
            
            if(vMenuId == "0") {                 
                 if(isAddNew == "0" || vUpMenuId != vOriUpMenuId) {
                    createSubTreeNodes(vDtNodeIndex,subMenuArray);
                 }                 
                 ctrlDtNode.setAttribute("EXPANDED",true);
            }
            else {
                expandParentNode(ctrlDtNode);
                if(vLoadSign == "0") {  // unexpended
		            var childNodes = ctrlDtNode.getChildren();
		            if(null == childNodes || childNodes == "undefined" || childNodes.length == 0) {
		                var oEmptyNode = $get("trvMenu").createTreeNode();  
	                    ctrlDtNode.add(oEmptyNode); 
		            }
		            ctrlDtNode.setAttribute("EXPANDED",true);
		        }
		        else {		 
	                if(isAddNew=="1"){//编辑
	                    if(vUpMenuId == vOriUpMenuId){ // parent node no change
	                        return;
	                    }
	                    else {		
	                        // add node to destination parent node		                
	                        createSubTreeNodes(vDtNodeIndex,subMenuArray);
	                    }   
	                }
	                else //新增
	                {		       	                
	                   createSubTreeNodes(vDtNodeIndex,subMenuArray);
	                }
        		    
	                ctrlDtNode.setAttribute("EXPANDED",true);	                
	            }
            }
            
			curNodeObj = null;			
		}
		
		function expandDtNode(dtNodeId) {
		    $get("_hidExpandSign").value = "1";//连续展开标志
		    PageMethods.GetNodeLevelStruct(dtNodeId,ClientCallBack);
		}
		
		function doDelTrigger() {
		    var delMenuId = $get("_hidDelMenuId").value;
		    ctrlNode = findTreeNode(delMenuId);
		    nodeIndex = ctrlNode.getNodeIndex();
		    var node = $get("trvMenu").getTreeNode(nodeIndex); 
            node.remove();            
		}
		
		function expandParentNode(dtNode) {
		    var dtParentNode = dtNode.getParent();
			dtParentNode.setAttribute("EXPANDED",true);
			
			var dtNodeId = dtParentNode.getAttribute("ID");
			var vNodeIdArray = dtNodeId.split("_")
			var vDeptId = vNodeIdArray[0];
			if (vDeptId != "0") {			    
			    expandParentNode(dtParentNode);
			}
			
		}
		
		function initialTreeNode(ctrlFindNode) {
		    var ctrlTree = document.getElementById("trvMenu");	
		    var vNodeId;
		    var vNodeIdArray;
		    var vMenuId;	
		    var vNodeIndex;
		    if(null != ctrlFindNode) {
		        vNodeId = ctrlFindNode.getAttribute("ID");    
		        vNodeIdArray = vNodeId.split("_")
		        vMenuId = vNodeIdArray[0];
		        ctrlFindNode.setAttribute("ID",vMenuId + "_" + "0");
		        vNodeIndex = ctrlFindNode.getNodeIndex();
		        ctrlTree.selectedNodeIndex = vNodeIndex;
		        /* 如果当前节点没有下级节点，则不会触发expand事件,
		         * 此时需要在当前节点下增加一个空节点 */
		        var vChildNodes = ctrlFindNode.getChildren();
		        if(vChildNodes.length == 0) {
		           var oEmptyNode = $get("trvMenu").createTreeNode();  
		           ctrlFindNode.add(oEmptyNode);
		        }
		        ctrlFindNode.setAttribute("EXPANDED",true);
		    }
		}
    //-->
    </script>
</body>
</html>
